package com.quizh.euro.server.ws;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;
import com.quizh.euro.server.dao.BoletoDAO;
import com.quizh.euro.shared.Boleto;

@Path("/init")
public class ServiceWs {
	
	@GET
	public String dame(@QueryParam("name") String user) {
		UserService userService = UserServiceFactory.getUserService();
		User cu = userService.getCurrentUser();
		BoletoDAO dao = new BoletoDAO();
		List<Boleto> result = dao.get(cu.getEmail());
		Gson gson = new Gson();
		String response = gson.toJson(result);
		return response;
	}
	
	@GET
	@Path("/search")
	public String search(@QueryParam("name") String user, @QueryParam("num") String num, @QueryParam("star") String star) {
		BoletoDAO dao = new BoletoDAO();
		List<String> numFilter = new ArrayList<String>();
		String [] values = num.split(",");
		for (String val : values) {
			numFilter.add(val);
		}
		List<String> starFilter = new ArrayList<String>();

		values = star.split(",");
		for (String val : values) {
			starFilter.add(val);
		}
		List<Boleto> result = dao.search(numFilter,starFilter);
		Gson gson = new Gson();
		String response = gson.toJson(result);
		return response;
	}
	
	@POST
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String set(@FormParam("name") String user,@FormParam("n1") String n1,@FormParam("n2") String n2,@FormParam("n3") String n3,
			@FormParam("n4") String n4,@FormParam("n5") String n5, @FormParam("e1") String e1,@FormParam("e2") String e2) {
//		UserService userService = UserServiceFactory.getUserService();
//		User cu = userService.getCurrentUser();
		BoletoDAO dao = new BoletoDAO();
		List<String> nums = new ArrayList<String>();
		List<String> star = new ArrayList<String>();
		nums.add(n1);nums.add(n2);nums.add(n3);nums.add(n4);nums.add(n5);
		star.add(e1);star.add(e2);
		Boleto boleto = new Boleto(user, System.currentTimeMillis(), nums, star);
		dao.save(boleto);
		
		Gson gson = new Gson();
		String response = gson.toJson(boleto);
		return response;
	}
	
}
